﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para FRMDevolucionMatPri.xaml
    /// </summary>
    public partial class FRMDevolucionMatPri : Window
    {
        public FRMDevolucionMatPri()
        {
            InitializeComponent();
        }

        private void DevolucionMatPri_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(txtSemana);
            
        }

        private void dtgOrdenes_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                string StrDatos1 = "";
                DataGridCell cell = DataGridHelper.GetCell(dtgOrdenes, dtgOrdenes.SelectedIndex, 0);
                int i = dtgOrdenes.SelectedIndex;

                int IntColumna = dtgOrdenes.CurrentCell.Column.DisplayIndex;
                StrDatos1 = cell.ToString().Replace("System.Windows.Controls.DataGridCell:", "").Trim();

                if (cell != null)
                {

                    if (IntColumna == 0)
                    {
                        if (StrDatos1.Length == 6)
                        {
                            if (i == -1)
                            {
                                i = 0;
                                dtgOrdenes.CurrentCell = new DataGridCellInfo(dtgOrdenes.Items[i], dtgOrdenes.Columns[0]);
                                dtgOrdenes.ScrollIntoView(dtgOrdenes.Items[i+1]);
                                dtgOrdenes.BeginEdit();
                            }
                            else
                            {
                                dtgOrdenes.CurrentCell = new DataGridCellInfo(dtgOrdenes.Items[i], dtgOrdenes.Columns[0]);
                                dtgOrdenes.ScrollIntoView(dtgOrdenes.Items[i+1]);
                                dtgOrdenes.BeginEdit();
                            }
                        }

                    }
                }
                else
                {
                    e.Handled = true;
                    elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        }

        private void dtgMateriales_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            string strError = "";
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                string StrDatos1 = "";
                DataGridCell cell = DataGridHelper.GetCell(dtgMateriales, dtgMateriales.SelectedIndex, 3);
                int i = dtgMateriales.SelectedIndex;

                int IntColumna = dtgMateriales.CurrentCell.Column.DisplayIndex;
                StrDatos1 = cell.ToString().Replace("System.Windows.Controls.DataGridCell:", "").Trim();

                if (cell != null)
                {

                    if (IntColumna == 3)
                    {
                        try
                        {
                            if (i == -1)
                            {
                                i = 0;
                                dtgMateriales.CurrentCell = new DataGridCellInfo(dtgMateriales.Items[i], dtgMateriales.Columns[3]);
                                dtgMateriales.ScrollIntoView(dtgMateriales.Items[i + 1]);
                                dtgMateriales.BeginEdit();
                            }
                            else
                            {
                                dtgMateriales.CurrentCell = new DataGridCellInfo(dtgMateriales.Items[i], dtgMateriales.Columns[3]);
                                dtgMateriales.ScrollIntoView(dtgMateriales.Items[i + 1]);
                                dtgMateriales.BeginEdit();
                            }
                        }
                        catch (Exception ex) { strError = ex.Message; }

                    }
                }

            }
        }

        //
        // SINGLE CLICK EDITING
        //
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }

        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        }

        private void DevolucionMatPri_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
            }
        }

        #region ParaObtenerLasPropiedadesDeLaCeldaDelGrid
        static class DataGridHelper
        {
            static public DataGridCell GetCell(DataGrid dg, int row, int column)
            {
                DataGridRow rowContainer = GetRow(dg, row);

                if (rowContainer != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

                    // try to get the cell but it may possibly be virtualized
                    DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    if (cell == null)
                    {
                        // now try to bring into view and retreive the cell
                        dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                    }
                    return cell;
                }
                return null;
            }

            static public DataGridRow GetRow(DataGrid dg, int index)
            {
                DataGridRow row = new DataGridRow();
                if (index != -1)
                {
                    row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
                    if (row == null)
                    {
                        // may be virtualized, bring into view and try again
                        dg.ScrollIntoView(dg.Items[index]);
                        row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
                    }
                }
                else
                {
                    row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(0);
                }
                return row;
            }

            static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
        }
        #endregion

        private void cmbProceso_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                dtgOrdenes.Focus();
                dtgOrdenes.CurrentCell = new DataGridCellInfo(dtgOrdenes.Items[0], dtgOrdenes.Columns[0]);
                dtgOrdenes.BeginEdit();
            }
            
        }
      
    }
}
